package com.hu.bgp.server;

import java.io.File;

import org.mortbay.jetty.servlet.DefaultServlet;
import org.mortbay.resource.Resource;

/**
 * Analyseert opgevraagde URL en serveert resources (plaatjes, gwt javascript
 * files etc.) van specifieke gametypes. Gebaseerd op Jetty klasse.
 * 
 * @author ejvos
 */
public class ResourceServlet extends DefaultServlet
{
	public Resource getResource(String pathInContext)
	{
		System.out.println("ResourceServlet.getResource");
		GameServerContext gsc = GameServerContext.getInstance(getServletContext());
		Resource resource = null;
		try
		{
			String contextPath = pathInContext;
			int firstQmark = contextPath.indexOf('?');
			if (firstQmark != -1)
				contextPath = contextPath.substring(0, firstQmark - 1);

			File file = gsc.getResourcesFileByContextPath(contextPath);
			System.out.println("ResourceServlet.getResource: " + file.getAbsolutePath() + " exists: " + file.exists());
			resource = Resource.newResource(file.getAbsolutePath());
		}
		catch (Throwable t)
		{
			// ignored, return null
		}

		return resource;
	}
}
